package org.ouwh.fw.model;

import java.io.InputStream;

import org.ouwh.fw.util.xml.Attribute;
import org.ouwh.fw.util.xml.Node;
import org.ouwh.fw.util.xml.XMLParser;

public abstract class AbstractLoader {

	abstract public Object loadObjecFromNode(Node xmlNode) throws Exception;

	private Node xmlRoot;

	protected AbstractLoader() {
	}

	protected AbstractLoader(String resourceName) throws Exception {
		xmlRoot = loadXMLResource(resourceName);
	}

	protected Object loadObjectByName(String name) throws Exception {
		return loadObjecFromNode(xmlRoot.getChildren3(new Attribute("id", name))[0]);
	}

	private static Node loadXMLResource(String resourceName) throws Exception {
		XMLParser parser = new XMLParser();
		InputStream in = null;
		try {
			in = AbstractLoader.class.getResourceAsStream(resourceName);
			return parser.parse(in);
		} finally {
			try {
				in.close();
			} catch (Exception e) {
			}
		}
	}

}
